<template>
  <div>
    <div class="foot">
      <span>总金额</span>
      <span>￥{{ $store.getters.getAllPrice }}</span>
      <div class="settleAccounts"
           @click="getpayForOrder">
        <router-link to>结算</router-link>
      </div>
    </div>
    <div class="orderTip"
         v-show="orderflag">
      <div class="f-mask"></div>
      <div class="tipcont">
        <p style="margin-bottom: 0.3rem">当前为非送餐时间，确定预约点餐吗？</p>
        <div class="btns">
          <a @click="cancelTip">取消</a><a class="sure"
             @click="sureTip">确定</a>
        </div>
      </div>
    </div>
    <div class="orderTip"
         v-show="orderflag1">
      <div class="f-mask"></div>
      <div class="tipcont">
        <p>当前为非送餐时间，确定结算吗？</p>
        <p style="color:#f00">（您点的{{lunchmsg}}，会在{{lunchmsg}}时间段送达）</p>
        <div class="btns">
          <a @click="cancelmoneyTip">取消</a><a class="sure"
             @click="moneyTip">确定</a>
        </div>
      </div>
    </div>
    <div class="orderTip"
         v-show="orderflag2">
      <div class="f-mask"></div>
      <div class="tipcont">
        <p style="margin-bottom: 0.3rem">当前非{{ordermsg}}时间，请重新选择</p>
        <div class="btns">
          <a class="sure"
             @click="reChoseClick">确定</a>
        </div>
      </div>
    </div>
    <div class="orderTip"
         v-show="orderflag3">
      <div class="f-mask"></div>
      <div class="tipcont">
        <p style="margin-bottom: 0.3rem"
           v-text="ordermessage"></p>
        <div class="btns">
          <a @click="cancelorderClick">取消</a><a class="sure"
             @click="orderClick">确定</a>
        </div>
      </div>
    </div>
    <div class="orderTip"
         v-show="orderflag4">
      <div class="f-mask"></div>
      <div class="tipcont">
        <p style="margin-bottom: 0.3rem">当前时间大于19:00，非点餐时间，请退出！</p>
        <div class="btns">
          <a class="sure"
             @click="reChoseClick">确定</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import urls from '../../../common.js'
import { Toast } from 'mint-ui'
export default {
  name: 'order',
  data () {
    return {
      orderflag: false,
      orderflag1: false,
      orderflag2: false,
      orderflag3: false,
      orderflag4: false,
      lunchflag: false,
      lunchflag1: false,
      ordermessage: '',
      ordermsg: '',
      lunchmsg: '',
      obj: {
        bed_no: this.$store.state.bedNo, // 床号
        case_number: this.$store.state.personinfo.pa_id, // pa_id
        consumer: this.$store.state.personinfo.pa_name, // 姓名
        delivery_address: this.$store.state.personinfo.dp_name, // 科室
        goodsInfo: this.$store.state.car, // 菜品信息
        in_time: this.$store.state.personinfo.in_time, // 住院次数
        pa_id: this.$store.state.personinfo.pa_id, // pa_id
        phone: this.$store.state.personinfo.phone, // 手机号
        restaurant_name: '清真餐厅', // 餐厅名
        total_amount: this.$store.getters.getAllPrice // 总金额
      },
      currentTime1: ''
    }
  },
  created () {
    // this.clearSettime = setInterval(() => {
    this.toOrderFive()
    // }, 30000)
  },
  methods: {
    // dateTimes () { // 调用main.js的时间
    //   let date = this.toLocaleString()
    //   date.then(function (datas) {
    //     let data = new Date(datas.body)
    //     this.dateObj.hours = this.addZero(data.getHours())
    //     this.dateObj.minutes = this.addZero(data.getMinutes())
    //     this.currentTime = this.dateObj.hours + ':' + this.dateObj.minutes
    //   })
    // },
    timeAdd0 (str) { // 时间不足补0
      if (str.length <= 1) {
        str = '0' + str
      }
      return str
    },
    localTimeFun () { // 获取本地时间
      let localHours = new Date().getHours().toString() // 时
      let localMin = new Date().getMinutes().toString() // 分
      this.localtime = this.timeAdd0(localHours) + ':' + this.timeAdd0(localMin)
      console.log(this.localtime)
    },
    serciceTime () { // 获取服务器时间
      let date = this.toLocaleString()
      date.then(function (datas) {
        let data = new Date(datas.body)
        let hour = this.addZero(data.getHours())
        let minutes = this.addZero(data.getMinutes())
        this.currentTime1 = hour + ':' + minutes
      })
    },
    toOrderFive () {
      // this.dateTimes()
      let date = this.toLocaleString()
      date.then(function (datas) {
        let data = new Date(datas.body)
        let hour = this.addZero(data.getHours())
        let minutes = this.addZero(data.getMinutes())
        let currentTime = hour + ':' + minutes
        console.log(currentTime)
        if (currentTime >= '07:30' && currentTime <= '08:40') {
          // this.$router.push('/orderOne')
        } else if (currentTime >= '11:00' && currentTime <= '13:30') {
          // this.$router.push('/orderOne')
        } else if (currentTime >= '17:30' && currentTime <= '19:00') {
          // this.$router.push('/orderOne')
        } else {
          this.orderflag = true
          // this.$router.push('/orderOne')
          // clearInterval(this.clearSettime)
        }
      })
    },
    getpayForOrder: function () {
      this.obj.goodsInfo = this.$store.state.car
      console.log(this.$store.state.car)
      if (this.$store.state.car.num === 'undefined' || this.$store.state.car.num > 0 || this.$store.getters.getAllPrice > 8) {
        var flag = this.localTimeJuage()
        if (flag) {
          return false
        }
        // 判断时间是否在点餐范围内
        let date = this.toLocaleString()
        date.then(function (datas) {
          let data = new Date(datas.body)
          let hour = this.addZero(data.getHours())
          let minutes = this.addZero(data.getMinutes())
          let currentTime = hour + ':' + minutes
          console.log(currentTime)
          if (currentTime >= '07:30' && currentTime <= '08:40') {
            let some = this.$store.state.car.some(function (item) { // 早餐时间，预约了午餐
              return item.type_id === 2 && item.num > 0
            })
            let some1 = this.$store.state.car.some(function (item) { // 早餐时间，预约了晚餐
              return item.type_id === 3 && item.num > 0
            })
            let some2 = this.$store.state.car.some(function (item) { // 早餐时间，预约了早餐
              return item.type_id === 1 && item.num > 0
            })
            if (some && some1 && some2) {
              this.orderflag3 = true
              this.ordermessage = '早餐即将送达，中餐和晚餐会在中餐和晚餐时间段送达'
            } else if (some && some1) {
              this.orderflag3 = true
              this.ordermessage = '中餐和晚餐会在中餐和晚餐时间段送达'
            } else if (some && some2) {
              this.orderflag3 = true
              this.ordermessage = '早餐即将送达，您所点的中餐，会在中餐时间段送达'
            } else if (some) {
              this.orderflag3 = true
              this.ordermessage = '您所点的中餐，会在中餐时间段送达'
            } else if (some1 && some2) {
              this.orderflag3 = true
              this.ordermessage = '早餐即将送达，您所点的晚餐，会在晚餐时间段送达'
            } else if (some1) {
              this.orderflag3 = true
              this.ordermessage = '您所点的晚餐，会在晚餐时间段送达'
            } else {
              this.moneyTip()
            }
          } else if (currentTime >= '11:00' && currentTime <= '13:30') {
            let some = this.$store.state.car.some(function (item) { // 只点了晚餐
              return item.type_id === 3 && item.num > 0
            })
            let some1 = this.$store.state.car.some(function (item) { // 点了午餐，预约了晚餐
              return item.type_id === 2 && item.num > 0
            })
            console.log(some)
            if (some && some1) {
              this.orderflag3 = true
              this.ordermessage = '中餐即将送达，您所点的晚餐，会在晚餐时间段送达'
            } else if (some) {
              this.orderflag3 = true
              this.ordermessage = '您所点的晚餐，会在晚餐时间段送达'
            } else {
              this.moneyTip()
            }
          } else if (currentTime >= '17:30' && currentTime <= '19:00') {
            this.moneyTip()
          } else { // 不在点餐时间范围内
            this.orderflag1 = true
            this.lunchflag = this.$store.state.car.some(function (item) { // 点了午餐
              return item.type_id === 2 && item.num > 0
            })
            this.lunchflag1 = this.$store.state.car.some(function (item) { // 点了晚餐
              return item.type_id === 3 && item.num > 0
            })
            this.lunchflag2 = this.$store.state.car.some(function (item) { // 点了早餐
              return item.type_id === 1 && item.num > 0
            })
            this.$store.state.car.forEach((item) => {
              if (this.lunchflag && this.lunchflag1 && this.lunchflag2) { // 点了午餐和晚餐
                this.lunchmsg = '早餐、中餐和晚餐'
              } else if (this.lunchflag && this.lunchflag2) {
                this.lunchmsg = '早餐和中餐'
              } else if (this.lunchflag1 && this.lunchflag2) {
                this.lunchmsg = '早餐和晚餐'
              } else if (this.lunchflag && this.lunchflag1) { // 中餐和晚餐
                this.lunchmsg = '中餐和晚餐'
              } else if (this.lunchflag) { // 点了午餐
                this.lunchmsg = '中餐'
              } else if (this.lunchflag1) { // 点了晚餐
                this.lunchmsg = '晚餐'
              } else if (this.lunchflag2) { // 点了早餐
                this.lunchmsg = '早餐'
              }
            })
          }
        })
      } else {
        Toast({
          message: '总金额不能少于8元',
          position: 'middle',
          duration: 2000
        })
      }
    },
    cancelTip () {
      this.orderflag = false
      this.$router.push('/orderOne')
    },
    sureTip () {
      this.orderflag = false
    },
    cancelmoneyTip () {
      this.orderflag1 = false
    },
    reChoseClick () {
      this.orderflag2 = false
      this.orderflag1 = false
      this.$router.push('/orderOne')
    },
    orderClick () {
      this.orderflag3 = false
      this.moneyTip()
    },
    cancelorderClick () {
      this.orderflag3 = false
    },
    moneyTip () {
      this.$http.post(urls.getpayForOrder, this.obj).then(function (result) {
        if (result.body.code === '200') {
          console.log(result.data)
          this.$router.push({
            name: 'orderThree',
            params: { key: result.data }
          })
          // clearInterval(this.clearSettime)
        }
      })
    },
    localTimeJuage () {
      // this.localTimeFun()// 获取本地时间
      this.serciceTime()
      // console.log(this.currentTime1)
      var flags = false
      if (this.currentTime1 > '19:00') {
        this.orderflag4 = true
        flags = true
      } else if (this.currentTime1 > '13:30' && this.currentTime1 <= '19:00') {
        this.someflag = this.$store.state.car.some(function (item) { // 点了早餐
          return item.type_id === 1 && item.num > 0
        })
        this.someflag1 = this.$store.state.car.some(function (item) { // 点了早餐
          return item.type_id === 2 && item.num > 0
        })
        if (this.someflag && this.someflag1) {
          this.orderflag2 = true
          this.ordermsg = '早餐、中餐'
          flags = true
        } else if (this.someflag) {
          this.orderflag2 = true
          this.ordermsg = '早餐'
          flags = true
        } else if (this.someflag1) {
          this.orderflag2 = true
          this.ordermsg = '中餐'
          flags = true
        }
        // this.$store.state.car.forEach((item) => {
        // if ((item.type_id === 1 || item.type_id === 2) && item.num > 0) {
        //   this.orderflag2 = true
        //   this.ordermsg = '中餐'
        //   flags = true
        // }
        // })
        return flags
      } else if (this.currentTime1 > '08:40' && this.currentTime1 <= '13:30') {
        this.$store.state.car.forEach((item) => {
          if (item.type_id === 1 && item.num > 0) {
            this.orderflag2 = true
            this.ordermsg = '早餐'
            flags = true
          }
        })
        return flags
      } else {
        this.orderflag2 = false
        return flags
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../../../static/css/color.styl'
.foot
  position absolute
  bottom 0.2rem
  left 0.2rem
  width 18.8rem
  height 0.8rem
  line-height 0.8rem
  border 1px solid #ccc
  border-radius 0.15rem
  background-color #fff
  span
    font-size 0.34rem
    &:nth-of-type(1)
      color $color-price
      margin 0 0.3rem
    &:nth-of-type(2)
      color $color-red
  .settleAccounts
    float right
    width 2rem
    height 0.78rem
    background-color $color-main
    border-radius 0.15rem
    border-top-left-radius 0
    border-bottom-left-radius 0
    text-align center
    a
      font-size 0.34rem
      color $color-white
      text-decoration none
.orderTip
  background #fff
  width 6rem
  height 2rem
  text-align center
  border-radius 0.2rem
  position fixed
  top 50%
  left 50%
  margin -1rem 0 0 -3rem
  .tipcont
    width 6rem
    height 2rem
    text-align center
    border-radius 0.2rem
    background #fff
    position relative
    z-index 10
    p
      &:nth-child(1)
        line-height 1
        padding 0.4rem 0 0.2rem 0
        font-size 0.24rem
      &:nth-child(2)
        line-height 1
        padding 0 0 0.2rem 0
        font-size 0.2rem
    a
      display inline-block
      width 1.2rem
      height 0.5rem
      line-height 0.5rem
      text-align center
      font-size 0.24rem
      color #27ae9e
      background #fff
      border 1px solid #27ae9e
      border-radius 0.05rem
      margin-left 0.2rem
    .sure
      background #27ae9e
      color #fff
.f-mask
  position fixed
  top 0
  left 0
  right 0
  bottom 0
  background rgba(0, 0, 0, 0.5)
</style>
